package cn.gary.oaserver.controller;


import cn.gary.oaserver.entity.TMenu;
import cn.gary.oaserver.params.Result;
import cn.gary.oaserver.service.ITMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gary
 * @since 2024-03-15
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单控制器类")
public class TMenuController {

    @Autowired
    ITMenuService menuService;


    @ApiOperation(value = "通过用户ID查询菜单列表")
    @GetMapping("/getList")
    public Result getList() {
        return Result.success ( "成功返回菜单", menuService.getMenusByAdminId ( ) );
    }

    @ApiOperation(value = "通过名称查询菜单列表")
    @GetMapping("/getAllMenuList")
    public Result getAllMenuList() {
        return Result.success ( "成功返回菜单", menuService.getMenus ( ) );
    }

    @ApiOperation(value = "保存菜单")
    @PostMapping("/saveMenu")
    public Result saveMenu(@RequestBody TMenu menu) {
        System.out.println (menu );
        String opr = "修改";
        if (menu.getId ( ) == null || menu.getId ( ).intValue ( ) == 0) {
            opr = "添加";
            menu.setRequireAuth ( true );
        }
        if (menuService.saveOrUpdateMenu ( menu )) {
            return Result.success ( opr + "菜单成功" );
        } else {
            return Result.error ( opr + "菜单失败" );
        }
    }

    @ApiOperation(value = "根据id删除菜单")
    @GetMapping("/delMenu/{id}")
    public Result delMenu(@PathVariable("id")Integer id) {
        if (menuService.delMenusById(id)){
            return Result.success ( "删除成功" );
        }else {
            return Result.error("删除失败");
        }
    }

}
